package com.hspedu.newData.dmsxl.栈;

import org.junit.Test;

import java.util.Stack;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-28 
 * @Version: 1.0
 */

public class LC1047删除字符串中的所有相邻重复项 {

    // todo 栈里的内容如何按顺序转成字符串
    public String removeDuplicates(String s) {


        StringBuilder stack =  new StringBuilder();
        int top = -1;
        for (int i = 0; i < s.length(); i++) {
            if (top >= 0 && s.charAt(i) == stack.charAt(top)){
                stack.deleteCharAt(top);
                top--;
            }else {
                stack.append(s.charAt(i));
                top++;
            }
        }
        return stack.toString();
    }

    @Test
    public void test() {
        // 测试代码
        removeDuplicates("abbaca");
    }
}
